package com.nuts.core.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.nuts.base.core.BaseServiceImpl;
import com.nuts.base.core.IBaseDao;
import com.nuts.core.bean.request.DimensionItemReqVO;
import com.nuts.core.bean.request.DimensionReqVO;
import com.nuts.core.bean.response.DimensionItemVO;
import com.nuts.core.bean.response.DimensionVO;
import com.nuts.core.dao.IDimensionDao;
import com.nuts.core.dao.IDimensionItemDao;
import com.nuts.core.service.IDimensionService;

/**
 * 维度业务实现
 * 
 * @author 许自辉
 * @date 2017年8月17日 下午3:35:08
 */
@Service
public class DimensionImpl extends BaseServiceImpl<DimensionReqVO, DimensionVO> implements IDimensionService {

	private IDimensionDao dimensionDao;

	@Autowired
	private IDimensionItemDao dimensionItemDao;

	@Autowired
	@Override
	public void setBaseDao(IBaseDao<DimensionReqVO, DimensionVO> baseDao) {
		this.baseDao = baseDao;
		this.dimensionDao = (IDimensionDao) baseDao;
	}
	
	@Override
	public int insert(DimensionReqVO record) {
		DimensionReqVO dimensionReqVO = new DimensionReqVO();
		dimensionReqVO.setCode(record.getCode());
		List<DimensionVO> ditems = dimensionDao.getList(record);
		if (CollectionUtils.isEmpty(ditems)) {
			return super.insert(record);
		}
		return -1;
	}

	@Override
	public int deleteById(int id) {
		DimensionItemReqVO dimensionItemReqVO = new DimensionItemReqVO();
		dimensionItemReqVO.setDimensionId(id);
		//先查询有没有数据范围
		List<DimensionItemVO> ditems = dimensionItemDao.getList(dimensionItemReqVO);
		if (CollectionUtils.isEmpty(ditems)) {
			return super.deleteById(id);
		}
		return -1;
	}

}
